class BookViewModel:
    # 单条的数据处理
    def __init__(self, book):
        self.title = book['title']
        self.author = '、'.join(book['author'])
        self.publisher = book['publisher']
        self.image = book['image']
        self.price = book['price']
        self.summary = book['summary']
        self.isbn = book['isbn']
        self.pages = book['pages']
        self.pubdate = book['pubdate']
        self.binding = book['binding']

    @property  # 将方法转换为相同名称的只读属性 使用方式和self.title一样
    def intro(self):
        # 如果列表中的元素数据存在，则保存在列表里，不存在则删除出列表
        intros = filter(lambda x: True if x else False,
                        [self.author, self.publisher, self.price])
        # 把列表中的元素以’/‘隔开
        return '/'.join(intros)


class BookCollection:
    def __init__(self):
        self.books = []
        self.total = 0
        self.keyword = ''

    def fill(self, yushu_book, keyword):
        self.books = [BookViewModel(book) for book in yushu_book.books]  # book 来源于yushu_book.books的实例属性
        self.total = yushu_book.total
        self.keyword = keyword


class _BookViewModel:
    '''
    这个类不用了
    面向对象
        描述特征（类变量，实例变量）
        行为（方法）
    '''

    @classmethod
    def package_single(cls, data, keyword):
        returned = {
            'books': [],
            'total': 0,
            'keyword': keyword
        }
        if data:
            returned['total'] = 1
            returned['books'] = [cls.__cut_book_data(data)]
        return returned

    @classmethod
    def package_collection(cls, data, keyword):
        returned = {
            'books': [],
            'total': 0,
            'keyword': keyword
        }
        if data:
            returned['total'] = len(data['books'])
            returned['books'] = [cls.__cut_book_data(book) for book in data['books']]
        return returned

    @classmethod
    def __cut_book_data(cls, data):
        '''
        :param data: 原始数据
        :return: 处理完的新数据返回给客户端
        '''
        book = {
            'title': data['title'],
            'author': '、'.join(data['author']),
            'publisher': data['publisher'],
            'image': data['image'],
            'price': data['price'],
            'summary': data['summary'] or '',
            'pages': data['pages'] or ''
        }
        return book
